Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
	''' <summary>
	''' Strongly-typed collection for the AspnetRoles class.
	''' </summary>
	<Serializable> _
	Public Partial Class AspnetRolesCollection 
	Inherits ActiveList(Of AspnetRoles, AspnetRolesCollection)
	    Public Sub New()
		End Sub
	End Class
	''' <summary>
	''' This is an ActiveRecord class which wraps the aspnet_Roles table.
	''' </summary>
	<Serializable> _
	Public Partial Class AspnetRoles 
	Inherits ActiveRecord(Of AspnetRoles)
		#Region ".ctors and Default Settings"
		
		Public Sub New()
			SetSQLProps()
			InitSetDefaults()
			MarkNew()
		End Sub
		
		Public Sub New(ByVal useDatabaseDefaults As Boolean)
			SetSQLProps()
			If useDatabaseDefaults = True Then
				ForceDefaults()
			End If
			MarkNew()
		End Sub
		Public Sub New(ByVal keyID As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByKey(keyID)
		End Sub
		Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByParam(columnName,columnValue)
		End Sub
		Private Sub InitSetDefaults()
			SetDefaults()
		End Sub
		
		Protected Shared Sub SetSQLProps()
			GetTableSchema()
		End Sub
		#End Region
		
		#Region "Schema and Query Accessor"
		
		Public Shared ReadOnly Property Schema() As TableSchema.Table
			Get
				If BaseSchema Is Nothing Then
					SetSQLProps()
				End If
				Return BaseSchema
			End Get
		End Property
		Private Shared Sub GetTableSchema()
			If (Not IsSchemaInitialized) Then
				'Schema declaration
				Dim schema As TableSchema.Table = New TableSchema.Table("aspnet_Roles", TableType.Table, DataService.GetInstance("ClubStarterKitData"))
				schema.Columns = New TableSchema.TableColumnCollection()
				schema.SchemaName = "dbo"
				'columns
				
                
                Dim colvarApplicationId As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarApplicationId.ColumnName = "ApplicationId"
                colvarApplicationId.DataType = DbType.Guid
                colvarApplicationId.MaxLength = 0
                colvarApplicationId.AutoIncrement = false
                colvarApplicationId.IsNullable = false
                colvarApplicationId.IsPrimaryKey = false
                colvarApplicationId.IsForeignKey = true
                colvarApplicationId.IsReadOnly = false
                
                
				colvarApplicationId.ForeignKeyTableName = "aspnet_Applications"
                
                schema.Columns.Add(colvarApplicationId)
                
                Dim colvarRoleId As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarRoleId.ColumnName = "RoleId"
                colvarRoleId.DataType = DbType.Guid
                colvarRoleId.MaxLength = 0
                colvarRoleId.AutoIncrement = false
                colvarRoleId.IsNullable = false
                colvarRoleId.IsPrimaryKey = true
                colvarRoleId.IsForeignKey = false
                colvarRoleId.IsReadOnly = false
                
						colvarRoleId.DefaultSetting = "(newid())"
				
                
                schema.Columns.Add(colvarRoleId)
                
                Dim colvarRoleName As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarRoleName.ColumnName = "RoleName"
                colvarRoleName.DataType = DbType.String
                colvarRoleName.MaxLength = 256
                colvarRoleName.AutoIncrement = false
                colvarRoleName.IsNullable = false
                colvarRoleName.IsPrimaryKey = false
                colvarRoleName.IsForeignKey = false
                colvarRoleName.IsReadOnly = false
                
                
                schema.Columns.Add(colvarRoleName)
                
                Dim colvarLoweredRoleName As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLoweredRoleName.ColumnName = "LoweredRoleName"
                colvarLoweredRoleName.DataType = DbType.String
                colvarLoweredRoleName.MaxLength = 256
                colvarLoweredRoleName.AutoIncrement = false
                colvarLoweredRoleName.IsNullable = false
                colvarLoweredRoleName.IsPrimaryKey = false
                colvarLoweredRoleName.IsForeignKey = false
                colvarLoweredRoleName.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLoweredRoleName)
                
                Dim colvarDescription As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarDescription.ColumnName = "Description"
                colvarDescription.DataType = DbType.String
                colvarDescription.MaxLength = 256
                colvarDescription.AutoIncrement = false
                colvarDescription.IsNullable = true
                colvarDescription.IsPrimaryKey = false
                colvarDescription.IsForeignKey = false
                colvarDescription.IsReadOnly = false
                
                
                schema.Columns.Add(colvarDescription)
                
				BaseSchema = schema
				
				'add this schema to the provider
                'so we can query it later
                DataService.Providers("ClubStarterKitData").AddSchema("aspnet_Roles",schema)
			End If
		End Sub
		Public Shared Function CreateQuery() As Query
			Return New Query(Schema)
		End Function
		
		#End Region
		
		#Region "Props"
	
          
        <XmlAttribute("ApplicationId")> _
        Public Property ApplicationId As Guid 
			Get
				Return GetColumnValue(Of Guid)("ApplicationId")
			End Get
		    
			Set
				SetColumnValue("ApplicationId", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("RoleId")> _
        Public Property RoleId As Guid 
			Get
				Return GetColumnValue(Of Guid)("RoleId")
			End Get
		    
			Set
				SetColumnValue("RoleId", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("RoleName")> _
        Public Property RoleName As String 
			Get
				Return GetColumnValue(Of String)("RoleName")
			End Get
		    
			Set
				SetColumnValue("RoleName", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("LoweredRoleName")> _
        Public Property LoweredRoleName As String 
			Get
				Return GetColumnValue(Of String)("LoweredRoleName")
			End Get
		    
			Set
				SetColumnValue("LoweredRoleName", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Description")> _
        Public Property Description As String 
			Get
				Return GetColumnValue(Of String)("Description")
			End Get
		    
			Set
				SetColumnValue("Description", Value)
			End Set
		End Property
		
		
		
		
		#End Region
		
		
			    
	    
	    #Region "PrimaryKey Methods"
	    
			Public Function AspnetUsersInRolesRecords() As Data.AspnetUsersInRolesCollection 
	
				Return New Data.AspnetUsersInRolesCollection().Where(AspnetUsersInRoles.Columns.RoleId, RoleId).Load()
	
			End Function
			
		#End Region
		
		
		
		
		
		
	    
	    #Region "ForeignKey Methods"
	    
		''' <summary>
		''' Returns a AspnetApplications ActiveRecord object related to this AspnetRoles
		''' </summary>
		Public Property AspnetApplications() As Data.AspnetApplications
			Get
				Return Data.AspnetApplications.FetchByID(Me.ApplicationId)
			End Get
			Set
				SetColumnValue("ApplicationId", Value.ApplicationId)
			End Set
		End Property
	    
	    #End Region
	    
		
	    
	    #Region "Many To Many Helpers"
	    
	     
        Public Function GetAspnetUsersCollection() As Data.AspnetUsersCollection 
	        Return AspnetRoles.GetAspnetUsersCollection(Me.RoleId)
		End Function
		
        Public Shared Function GetAspnetUsersCollection(ByVal varRoleId As Guid) As Data.AspnetUsersCollection
        
            Dim cmd As SubSonic.QueryCommand = New SubSonic.QueryCommand("SELECT * FROM aspnet_Users INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId=aspnet_UsersInRoles.UserId WHERE aspnet_UsersInRoles.RoleId=@RoleId", AspnetRoles.Schema.Provider.Name)
            
            cmd.AddParameter("@RoleId", varRoleId, DbType.Guid)
            Dim rdr As IDataReader = SubSonic.DataService.GetReader(cmd)
            Dim coll As AspnetUsersCollection = New AspnetUsersCollection()
            coll.LoadAndCloseReader(rdr)
            Return coll
            
        End Function
        
        Public Shared Sub SaveAspnetUsersMap(ByVal varRoleId As Guid, ByVal items As AspnetUsersCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE RoleId=@RoleId", AspnetRoles.Schema.Provider.Name)
            cmdDel.AddParameter("@RoleId", varRoleId)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            For Each item As AspnetUsers In items 
				Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
				varAspnetUsersInRoles.SetColumnValue("RoleId", varRoleId)
				varAspnetUsersInRoles.SetColumnValue("UserId", item.GetPrimaryKeyValue())
				varAspnetUsersInRoles.Save()
			Next
			
        End Sub
        
        
        Public Shared Sub SaveAspnetUsersMap(ByVal varRoleId As Guid, ByVal itemList As System.Web.UI.WebControls.ListItemCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE RoleId=@RoleId", AspnetRoles.Schema.Provider.Name)
            cmdDel.AddParameter("@RoleId", varRoleId)
            'add this in
            coll.Add(cmdDel)
            DataService.ExecuteTransaction(coll)
            For Each l As System.Web.UI.WebControls.ListItem In itemList 
            
                If l.Selected
				    
					Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
					varAspnetUsersInRoles.SetColumnValue("RoleId", varRoleId)
					varAspnetUsersInRoles.SetColumnValue("UserId", l.Value)
					varAspnetUsersInRoles.Save()
				    
                End If
            Next
        End Sub
        
        Public Shared Sub SaveAspnetUsersMap(ByVal varRoleId As Guid, ByVal itemList() As Guid) 
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE RoleId=@RoleId", AspnetRoles.Schema.Provider.Name)
            cmdDel.AddParameter("@RoleId", varRoleId)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            
            For Each item As Guid In itemList
            
                	Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
					varAspnetUsersInRoles.SetColumnValue("RoleId", varRoleId)
					varAspnetUsersInRoles.SetColumnValue("UserId", item)
					varAspnetUsersInRoles.Save()
            Next
            
        End Sub
        
		Public Shared Sub DeleteAspnetUsersMap(ByVal varRoleId As Guid) 
		
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE RoleId=@RoleId", AspnetRoles.Schema.Provider.Name)
            cmdDel.AddParameter("@RoleId", varRoleId)
            DataService.ExecuteQuery(cmdDel)
        End Sub
	      
	      
	      
	    
	    #End Region
	    
		
		#Region "ObjectDataSource support"
		
		''' <summary>
		''' Inserts a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Insert(ByVal varApplicationId As Guid,ByVal varRoleId As Guid,ByVal varRoleName As String,ByVal varLoweredRoleName As String,ByVal varDescription As String)
			Dim item As AspnetRoles = New AspnetRoles()
			
            item.ApplicationId = varApplicationId
            
            item.RoleId = varRoleId
            
            item.RoleName = varRoleName
            
            item.LoweredRoleName = varLoweredRoleName
            
            item.Description = varDescription
            
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		''' <summary>
		''' Updates a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Update(ByVal varApplicationId As Guid,ByVal varRoleId As Guid,ByVal varRoleName As String,ByVal varLoweredRoleName As String,ByVal varDescription As String)
			Dim item As AspnetRoles = New AspnetRoles()
		    
                item.ApplicationId = varApplicationId
				
                item.RoleId = varRoleId
				
                item.RoleName = varRoleName
				
                item.LoweredRoleName = varLoweredRoleName
				
                item.Description = varDescription
				
			item.IsNew = False
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		#End Region
		#Region "Columns Struct"
		Public Structure Columns
			Dim x as Integer
			
            Public Shared ApplicationId As String = "ApplicationId"
            
            Public Shared RoleId As String = "RoleId"
            
            Public Shared RoleName As String = "RoleName"
            
            Public Shared LoweredRoleName As String = "LoweredRoleName"
            
            Public Shared Description As String = "Description"
            
		End Structure
		#End Region
	End Class
End Namespace
